package com.douding.server.service;

import com.douding.server.domain.Teacher;
import com.douding.server.domain.TeacherExample;
import com.douding.server.dto.PageDto;
import com.douding.server.dto.TeacherDto;
import com.douding.server.mapper.TeacherMapper;
import com.douding.server.util.CopyUtil;
import com.douding.server.util.UuidUtil;
import com.douding.server.util.ValidatorUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class TeacherService {

    @Resource
    private TeacherMapper teacherMapper;


    /**
     * 列表查询
     */
    public void list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        TeacherExample teacherExample = new TeacherExample();
        List<Teacher> teacherList = teacherMapper.selectByExample(teacherExample);
        PageInfo<Teacher> pageInfo = new PageInfo<>(teacherList);
        pageDto.setTotal(pageInfo.getTotal());
        List<TeacherDto> teacherDtoList = CopyUtil.copyList(teacherList, TeacherDto.class);
        pageDto.setList(teacherDtoList);
    }

    public void preCheck(TeacherDto teacherDto) {
        // 检查字段是否为空
        ValidatorUtil.require(teacherDto.getName(), "教师姓名");
        ValidatorUtil.require(teacherDto.getImage(), "教师头像");
        ValidatorUtil.require(teacherDto.getIntro(), "教师简介");
        ValidatorUtil.require(teacherDto.getMotto(), "教师座右铭");
        ValidatorUtil.require(teacherDto.getNickname(), "教师昵称");
        ValidatorUtil.require(teacherDto.getIntro(), "教师职位");
        // 检查字段格式是否正确
        ValidatorUtil.require(teacherDto.getName(), "教师姓名", String.class);
        ValidatorUtil.require(teacherDto.getImage(), "教师头像", String.class);
        ValidatorUtil.require(teacherDto.getIntro(), "教师简介", String.class);
        ValidatorUtil.require(teacherDto.getMotto(), "教师座右铭", String.class);
        ValidatorUtil.require(teacherDto.getNickname(), "教师昵称", String.class);
        ValidatorUtil.require(teacherDto.getIntro(), "教师职位", String.class);
    }

    public void save(TeacherDto teacherDto) {
        preCheck(teacherDto);
        String id = teacherDto.getId();
        Teacher teacher = CopyUtil.copy(teacherDto, Teacher.class);
        // 如果id为空，则进行修改操作
        if (!StringUtils.isEmpty(id)) {
            update(teacher);
            return;
        }
        // 进行插入操作
        insert(teacher);
    }

    //新增数据
    private void insert(Teacher teacher) {
        teacher.setId(UuidUtil.getShortUuid());
        teacherMapper.insert(teacher);
    }

    //更新数据
    private void update(Teacher teacher) {
        teacherMapper.updateByPrimaryKeySelective(teacher);
    }

    public void delete(String id) {
        teacherMapper.deleteByPrimaryKey(id);
    }

    public List<TeacherDto> all() {
        List<Teacher> teacherList = teacherMapper.selectAll();
        return CopyUtil.copyList(teacherList, TeacherDto.class);
    }


    /**
     * 查找
     * @param id
     */
    public TeacherDto findById(String id) {
        return null;
    }
}//end class
